<?php
class ModelWpCategory extends Model {
	
	public function getList($customer_id){
		$sql = "select a.*,(c.id is not null) as chosen
				from wp_terms a 
				join wp_term_taxonomy b on a.term_id = b.term_id
				left join customer_wpcategory c on b.term_id = c.category_id and c.customer_id={$customer_id}

				where b.taxonomy = 'category' and b.parent = 0 ";
				
		
		$query = $this->db->query($sql);

		return $query->rows;
	}
	
	public function getMyCategoryList($customer_id){
		$sql = "select a.* from wp_terms a join wp_term_taxonomy b on a.term_id = b.term_id
				join customer_wpcategory c on b.term_id = c.category_id and c.customer_id={$customer_id}
				where b.taxonomy = 'category' and b.parent = 0";
				
		
		$query = $this->db->query($sql);

		return $query->rows;
	}
	
	public function bind($customer_id, $category_list){
		if(count($category_list) == 0)
			return;
			
		$this->db->query("delete from customer_wpcategory where customer_id={$customer_id}");
			
		$sql = "insert into customer_wpcategory(customer_id,category_id) ";
		
		$first = true;
		foreach($category_list as $category_id){
			if($first){
				$sql .= "select {$customer_id},{$category_id} ";
			}
			else{
				$sql .= "union all select {$customer_id},{$category_id} ";
			}
			$first = false;
		}
		
		
		
		$this->db->query($sql);

		
	}
	
	
	
}